<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { authService } from '@/services/auth.js'

const router = useRouter()
const formData = ref({
  username: '',
  password: ''
})
const errorMessage = ref('')
const isLoading = ref(false)

// 示例用户名和密码，方便用户快速测试
formData.value.username = 'admin'
formData.value.password = 'admin123'

const handleLogin = async () => {
  // 简单的表单验证
  if (!formData.value.username || !formData.value.password) {
    errorMessage.value = '请填写所有必填字段'
    return
  }

  isLoading.value = true
  errorMessage.value = ''

  try {
    // 使用auth服务进行登录
    const result = await authService.login(formData.value.username, formData.value.password)
    
    console.log('登录成功', result)
    
    // 登录成功后跳转到首页
    router.push('/')
  } catch (error) {
    errorMessage.value = error.message || '登录失败，请检查用户名和密码'
    console.error('登录错误:', error)
  } finally {
    isLoading.value = false
  }
}

const goToRegister = () => {
  router.push('/register')
}
</script>

<template>
  <div class="auth-container">
    <div class="auth-card">
      <h2 class="auth-title">用户登录</h2>
      
      <form @submit.prevent="handleLogin" class="auth-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="formData.username"
            placeholder="请输入用户名"
            class="form-input"
            :disabled="isLoading"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="formData.password"
            placeholder="请输入密码"
            class="form-input"
            :disabled="isLoading"
          />
        </div>
        
        <div class="form-actions">
          <button
            type="submit"
            class="btn-primary"
            :disabled="isLoading"
          >
            {{ isLoading ? '登录中...' : '登录' }}
          </button>
        </div>
        
        <div class="form-footer">
          <p>还没有账号？ <a href="#" @click.prevent="goToRegister" class="link">立即注册</a></p>
        </div>
      </form>
      
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </div>
  </div>
</template>

<style scoped>
.auth-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: var(--color-background-soft);
}

.auth-card {
  background: var(--color-background);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  padding: 2rem;
  width: 100%;
  max-width: 400px;
}

.auth-title {
  color: var(--color-heading);
  text-align: center;
  margin-bottom: 1.5rem;
  font-size: 1.5rem;
  font-weight: 600;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.form-group label {
  color: var(--color-text);
  font-weight: 500;
  font-size: 0.9rem;
}

.form-input {
  padding: 0.75rem 1rem;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  font-size: 1rem;
  background-color: var(--color-background);
  color: var(--color-text);
  transition: border-color 0.2s;
}

.form-input:focus {
  outline: none;
  border-color: hsla(160, 100%, 37%, 1);
}

.form-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.form-actions {
  margin-top: 1.5rem;
}

.btn-primary {
  width: 100%;
  padding: 0.75rem 1rem;
  background-color: hsla(160, 100%, 37%, 1);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.2s;
}

.btn-primary:hover:not(:disabled) {
  background-color: hsla(160, 100%, 30%, 1);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.form-footer {
  text-align: center;
  margin-top: 1rem;
  font-size: 0.9rem;
  color: var(--color-text-light-2);
}

.link {
  color: hsla(160, 100%, 37%, 1);
  text-decoration: none;
  transition: opacity 0.2s;
}

.link:hover {
  opacity: 0.8;
}

.error-message {
  margin-top: 1rem;
  padding: 0.75rem;
  background-color: rgba(255, 0, 0, 0.1);
  color: #ff4d4f;
  border-radius: 4px;
  font-size: 0.9rem;
  text-align: center;
}
</style>